//瀑布流处理文件
export default {
	props: ["columnNum", "list", 'isGroupBuy', 'isNov'],
	watch: {
		list(data1, data2) {
			console.log("---------------watch")
			//if(this.lastChildCount!=this.list.length){
			// this.queryHeight();
			this.$nextTick(this.queryHeight)
			//}
		}
	},
	data() {
		return {
			defColumnNum: 2, //默认列数，如果不传，则默认两列
			columnWidth: 0,

			lastChildCount: 0,

			tabList: [],
			imageWidth: 0,

			imageHeight: 0
		}
	},
	mounted() {
		// console.log("------------mounted")
		// this.queryHeight();
		this.$nextTick(() => {
			this.queryHeight();
			// 获取图片宽高比例 a4纸比例 1:1.4
			const getScreenWidth = this.$u.sys().windowWidth;
			const boxWidth = getScreenWidth / 2
			const boxWidthUpx = boxWidth / (uni.upx2px(boxWidth) / boxWidth)
			this.imageWidth = boxWidthUpx - 10 * 3 - 28 * 2;
			this.imageHeight = this.imageWidth * 1.4;
		})
	},
	methods: {
		queryHeight() {
			if (this.columnNum == null || this.columnNum == "") {
				this.columnNum = this.defColumnNum;
			}
			this.columnWidth = 100 / this.columnNum;

			var that = this;
			var temHeightList = {};
			const query = uni.createSelectorQuery().in(this);
			for (var i = 0; i < this.list.length; i++) {
				query.select('#child' + i).boundingClientRect(data => {
					if (!data) return false
					temHeightList[data.id] = data.height;

					if (data.id == ('child' + (that.list.length - 1))) {
						that.refreshLayout(temHeightList);
					}
				}).exec();
			}
		},
		refreshLayout(listHeight) {
			var temTabList = [];
			var temTabHeightList = [];
			for (var i = 0; i < this.columnNum; i++) {
				var temArr = [];
				temTabList[i] = temArr;
				temTabHeightList[i] = 0;
			}


			for (var i = 0; i < this.list.length; i++) {
				var item = this.list[i];
				var itemHeight = listHeight['child' + i];

				var minHeight = 1000000;
				var minIndex = 0;
				for (var j = 0; j < temTabHeightList.length; j++) {
					var tempHeight = temTabHeightList[j];
					if (tempHeight < minHeight) {
						minHeight = tempHeight;
						minIndex = j;
					}
				}
				temTabList[minIndex].push(item);
				temTabHeightList[minIndex] += itemHeight;
			}

			this.tabList = temTabList;

			this.lastChildCount = this.list.length;
			console.log("=======刷新完成======", temTabList)
		},
		goBuy(item) {
			this.$emit('goBuy', item)
		},
		
		//添加到购物车
		addCar(item) {
			this.$emit('addCar', item)
		},
		// 预览banner
		previewBanner(images) {
			const banner = images.split(',');
			this.previewUrls(banner)
		},
	}
}